/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package 解析.node.charproperty.支持补集;

import java.util.ArrayList;
import 解析.node.charproperty.CharProperty;

/**
 *
 * @author yanxi
 */
public class CharProperty交并集节点 extends CharProperty支持补集{
    public boolean is并集=true;
    public ArrayList<CharProperty> list=new ArrayList();  
    
    public void add(CharProperty node){
        if(node instanceof CharProperty交并集节点){
            CharProperty交并集节点 n=(CharProperty交并集节点)node;
            if(!n.isComplement){//不是补集才可以展开
                if(n.list.size()==1){ 
                    this.add(n.list.get(0));//将子节点加入，减少层次
                    return ;
                }
                else{
                    if(this.is并集==n.is并集){//是同样类型，则将子节点分别加入，减少层次
                        for(CharProperty cpn:n.list){
                            this.add(cpn);
                        }
                        return ;
                    }
                }
            } 
        }
        list.add(node);
        getChildren().add(node);
    } 
    
    @Override
    public boolean isSatisfiedBy正集(int ch) { 
        if(is并集) return isSatisfiedBy并集(ch); 
        else return isSatisfiedBy交集(ch); 
    }
 
    public boolean isSatisfiedBy并集 (int ch) {
        for(CharProperty node:list){
            if(node.isSatisfiedBy(ch))return true;
        }
        return false;
    }
    
    public boolean isSatisfiedBy交集 (int ch) {
        for(CharProperty node:list){
            if(!node.isSatisfiedBy(ch))return false;
        }
        return true;
    }
    
    @Override
    public String toString正集(){
        String s="[";
        if(is并集){
            for(CharProperty node:this.list){
                s+=node.toString();
            }
        }
        else{
            s+=this.list.get(0).toString();
            for(int i=1;i<list.size();i++){
                s+="&&"+list.get(i).toString();
            }
        }
        s+="]";
        return s;
    }
    @Override
    public String toString补集(){
        String s="[^";
        if(is并集){
            for(CharProperty node:this.list){
                s+=node.toString();
            }
        }
        else{
            s+=this.list.get(0).toString();
            for(int i=1;i<list.size();i++){
                s+="&&"+list.get(i).toString();
            }
        }
        s+="]";
        return s;
    }
}
